package cn.com.paycn.ken.utils;

import java.io.File;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import cn.com.paycn.ken.hbm.Member;

public class ResourceUtils
{
  public static final String SESSION_INFO = "session_info";
  public static final Integer DEFAULT_ROWS = Integer.valueOf(20);

  public static String TEMPLATE_DIR = "";

  public static boolean USE_REDIS = true;

  public static String SYSTEM_TYPE = "";

  public static String DEFAULT_PASSWARD = "";
  public static int DEFAULT_ROLE_ADMIN;
  public static int DEFAULT_ROLE_GM;
  public static int DEFAULT_ROLE_DM;
  public static int DEFAULT_ROLE_EM;

  public void setTemplateDir(String templateDir)
  {
    TEMPLATE_DIR = templateDir;
  }

  public void setSystemType(String systemType) {
    SYSTEM_TYPE = systemType;
  }

  public void setDefaultPassward(String defaultPassward) {
    DEFAULT_PASSWARD = defaultPassward;
  }

  public void setDefaultRoleAdmin(int defaultRoleAdmin) {
    DEFAULT_ROLE_ADMIN = defaultRoleAdmin;
  }

  public void setDefaultRoleGm(int defaultRoleGm) {
    DEFAULT_ROLE_GM = defaultRoleGm;
  }

  public void setDefaultRoleDm(int defaultRoleDm) {
    DEFAULT_ROLE_DM = defaultRoleDm;
  }

  public void setDefaultRoleEm(int defaultRoleEm) {
    DEFAULT_ROLE_EM = defaultRoleEm;
  }

  public void setUseRedis(Boolean useRedis) {
    if (useRedis == null)
      useRedis = Boolean.valueOf(true);
    USE_REDIS = useRedis.booleanValue();
  }

  public static String rootPath()
  {
    File clzPath = new File(ResourceUtils.class.getResource("/").getPath());
    return clzPath.getParentFile().getParent();
  }

  public static String staticPath()
  {
    String root = rootPath();
    return new File(root, "static").getAbsolutePath();
  }

  public static String staticChildPath(String child)
  {
    String root = rootPath();
    if (!child.startsWith("/")) {
      child = "/" + child;
    }
    return new File(root, "static" + child).getAbsolutePath();
  }

  public static Member currentMember(HttpSession session)
  {
    Object obj = session.getAttribute("session_info");
    if (obj != null)
      return (Member)obj;
    return null;
  }

  public static Member currentMember(HttpServletRequest request) {
    return currentMember(request.getSession());
  }
}